import win32gui

from PySide2 import QtCore
from PySide2.QtCore import Qt,QSettings
from PySide2.QtWidgets import QWidget,QAction,QApplication
from PySide2.QtGui import QShowEvent,QHideEvent,QPaintEvent,QPainter,QBrush,QPen,QIcon,QFont,QWindow

from UI.ui_taskusewidget import Ui_TaskUseWidget


class TaskUseWidget(QWidget):

    def __init__(self, parent: QWidget= None) -> None:
        super(TaskUseWidget, self).__init__(parent)

        self.ui=Ui_TaskUseWidget()
        self.ui.setupUi(self)

        self.__InitData()
        self.__InitUI()
        self.__InitStyle()
        self.__InitSignalAndSlot()

    def __del__(self):
        # b = win32gui.GetWindowRect(self.m_hReBar)  # 获取m_hBar窗口尺寸b为[左，上，右，下]的数组
        win32gui.MoveWindow(self.m_hTaskSw, 0, 0, self.ReBarRect[2] - self.ReBarRect[0], self.ReBarRect[3] - self.ReBarRect[1], True)


    def __InitData(self):
        self.Settings = QSettings(self)
        # self.Color=self.Settings.value("Global/TaskUIColor",QColor(210,226,255))
        self.ViewModel=self.Settings.value("Global/TaskViewModel",0)

        self.slotSwitchActionClicked(True)

        self.setContextMenuPolicy(Qt.ContextMenuPolicy.ActionsContextMenu)

        # 按钮定义
        self.HomeUIControlAction = QAction(QIcon(":/Image/Home.png"), self.tr("HomeUIControl"))
        self.SwitchAction = QAction(QIcon(":/Image/Show.png"), self.tr("SwitchView"))
        # self.ColorAction = QAction(QIcon(":/Image/Color.png"), self.tr("SetColor"))
        self.RefreshAction = QAction(QIcon(":/Image/Refresh.png"), self.tr("RefreshUI"))
        self.ExitAction = QAction(QIcon(":/Image/Exit.png"), self.tr("ExitApp"))

        # 添加按钮到菜单
        self.addAction(self.HomeUIControlAction)
        self.addAction(self.SwitchAction)
        # self.addAction(self.ColorAction)
        self.addAction(self.RefreshAction)
        self.addAction(self.ExitAction)


    # def slotColorActionClicked(self):
    #     color= QColorDialog.getColor(self.Color,self)
    #     if color.value()==0:
    #         return
    #     self.Color=color
    #     self.Settings.setValue("Global/TaskUIColor", self.Color)
    #     self.update()

    def slotSwitchActionClicked(self,Init:bool):

        if not Init:
            viewModel=self.ViewModel+1
            self.ViewModel=viewModel%3

        if self.ViewModel==0:
            self.ui.widget_Net.show()
            self.ui .widget_Used.show()
        elif self.ViewModel==1:
            self.ui.widget_Net.show()
            self.ui.widget_Used.hide()
        elif self.ViewModel==2:
            self.ui.widget_Net.hide()
            self.ui.widget_Used.show()

        print(self.ViewModel)

        self.Settings.setValue("Global/TaskViewModel", self.ViewModel)
        pass

    def __InitUI(self):
        self.setWindowFlags(Qt.Tool|Qt.FramelessWindowHint)

        # Shell_TrayWnd>ReBarWindow32>(包含)MSTaskSwWClass
        self.m_hTrayWnd =win32gui.FindWindow("Shell_TrayWnd", None)
        self.m_hReBar = win32gui.FindWindowEx(self.m_hTrayWnd, 0, "ReBarWindow32", None)
        self.m_hTaskSw = win32gui.FindWindowEx(self.m_hReBar, 0, "MSTaskSwWClass", None)

        win32gui.SetParent(int(self.winId()), self.m_hReBar)  # 设置任务栏为此窗口的父窗口

        self.ReBarRect = win32gui.GetWindowRect(self.m_hReBar)  # 获取m_hBar窗口尺寸为[左，上，右，下]的数组




    def __InitStyle(self):
        MenuStyleStr=str("QMenu {background-color:rgba(255,255,255,0.9);padding:5px;border-radius: 5px;}\
                QMenu::item {min-width:50px;padding:5px;margin:1px;border-radius: 3px;}\
                QMenu::item:selected {background:rgba(210,235,255,1);border-radius: 3px;}\
                QMenu::item:pressed {background:rgba(210,235,255,0.5);border:1px solid rgba(82,130,164,1);border-radius: 3px;}")

        self.setFont(QFont("Sim-Sun",9,QFont.Normal,False))

        self.setStyleSheet(MenuStyleStr)

    def __InitSignalAndSlot(self):

        # 按钮信号槽链接
        self.HomeUIControlAction.triggered.connect(lambda: self.signalHomeUIControl.emit())
        self.SwitchAction.triggered.connect(lambda: self.slotSwitchActionClicked(False))
        # self.ColorAction.triggered.connect(self.slotColorActionClicked)
        self.RefreshAction.triggered.connect(lambda: (self.hide(), self.show()))
        self.ExitAction.triggered.connect(lambda: QApplication.quit())


    def showEvent(self, event:QShowEvent) -> None:
        super(TaskUseWidget, self).showEvent(event)
        # b = win32gui.GetWindowRect(self.m_hReBar)  # 获取m_hBar窗口尺寸b为[0左，1上，2右，3下]的数组
        win32gui.MoveWindow(self.m_hTaskSw, 0, 0, self.ReBarRect[2] - self.ReBarRect[0] - 150, self.ReBarRect[3] - self.ReBarRect[1], True)
        self.setGeometry(self.ReBarRect[2] - self.ReBarRect[0] - 150, 1, 150, self.ReBarRect[3] - self.ReBarRect[1]-2)

    def hideEvent(self, event:QHideEvent) -> None:
        super(TaskUseWidget, self).hideEvent(event)
        # b =win32gui.GetWindowRect(self.m_hReBar)  # 获取m_hBar窗口尺寸b为[左，上，右，下]的数组
        win32gui.MoveWindow(self.m_hTaskSw, 0, 0, self.ReBarRect[2] - self.ReBarRect[0], self.ReBarRect[3] - self.ReBarRect[1], True)

    def updateRect(self):
        self.tempRect=self.ReBarRect
        self.ReBarRect = win32gui.GetWindowRect(self.m_hReBar)  # 获取m_hBar窗口尺寸为[左，上，右，下]的数组

        if self.tempRect!=self.ReBarRect:
            if not self.isHidden():
                win32gui.MoveWindow(self.m_hTaskSw, 0, 0, self.ReBarRect[2] - self.ReBarRect[0] - 150,self.ReBarRect[3] - self.ReBarRect[1], True)
                self.setGeometry(self.ReBarRect[2] - self.ReBarRect[0] - 150, 1, 150,self.ReBarRect[3] - self.ReBarRect[1] - 2)

    def SetCPUUseValue(self,inValue:int):
        self.ui.label_CPUValue.setText(str(inValue)+"%")

    def SetMemUseValue(self,inValue:int):
        self.ui.label_MemoryValue.setText(str(inValue)+"%")

    def SetNetSentValue(self,Sent:str):
        self.ui.label_NetSentValue.setText(Sent)

    def SetNetRecValue(self,Rec:str):
        self.ui.label_NetRecValue.setText(Rec)

    def paintEvent(self, event: QPaintEvent) -> None:
        super().paintEvent(event)

        painter = QPainter(self)
        # 设置混合模式，用于界面透明
        painter.setCompositionMode(QPainter.CompositionMode_Clear)
        painter.setRenderHint(QPainter.Antialiasing)
        # painter.setBrush(QBrush(self.Color))
        painter.setBrush(QBrush(Qt.transparent))
        painter.setPen(QPen(Qt.transparent))
        # painter.drawRoundedRect(self.rect(),6,6)
        painter.drawRect(self.rect())

    signalHomeUIControl=QtCore.Signal()
